package com.lenovo.leos.filebrowser.engine.operation;

import java.io.File;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Message;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lenovo.leos.filebrowser.engine.util.FileHandler;
import com.lenovo.leos.filebrowser.engine.util.FileThread;
import com.lenovo.leos.filebrowser.engine.util.FileUtil;
import com.lenovo.leos.filebrowser.ui.BaseActivity;
import com.lenovo.leos.filebrowser.ui.Global;
import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.engine.util.Log;

public class FileProperty {
	private static final String TAG = "FileProperty";
	
	// constant
	public static final int MSG_RES_OK = R.string.msg_property_res_ok;
	public static final int MSG_RES_FAILED = R.string.msg_property_res_failed;
	
	private static final int MSG_PROP_UPDATE = R.string.msg_property_update;
	
	// member
	private static BaseActivity sActivity = null; 
	
	private static int siSubDirNum = 0;
	private static int siSubFileNum = 0;
	private static long slFileSize = 0;
	
	private static TextView stvSubFileNum = null;
	private static TextView stvFileSize = null;
	private static TextView stvSubDirNum = null;
	
	private static FilePropertyThread sPropertyThread = null;
	
	private static FileHandler.MsgHandler sUpdateHandler = new FileHandler.MsgHandler(MSG_PROP_UPDATE, 
			new FileHandler.MsgHandleFunc(){

//		@Override
		public boolean onMessage(Message msg) {
			Log.i(TAG, "==> onMessage");
			// TODO Auto-generated method stub
			if (null != stvSubFileNum)
				stvSubFileNum.setText(String.valueOf(siSubFileNum));
			if (null != stvFileSize)
				stvFileSize.setText(FileUtil.getFileSizeString(slFileSize));
			if (null != stvSubDirNum)
				stvSubDirNum.setText(String.valueOf(siSubDirNum));
			return false;
		}});
	
	
	public static class FilePropertyThread extends FileThread {
		private static final String TAG = "FilePropertyThread";
		
		private File mfile = null;

		public FilePropertyThread(BaseActivity ba, File file) {
			super(ba, ba.getHandler());
			if (null == file || !file.exists()){
				Log.e(TAG,"file is invalid in FilePropertyThread");
				return;
			}else
				mfile = file;
		}

		public void onUpdateProp() {
			Log.i(TAG, "==> onUpdateProp");
			sendMsg(MSG_PROP_UPDATE);
		}
		
		protected void realRun() {
			super.realRun();
			getProperty(mfile);
		}
	}

	private static void getProperty(File file) {
		if (file.isFile() && !file.isHidden()) {
			siSubFileNum++;
			slFileSize += file.length();
			if (siSubFileNum % 10 == 0)
				sPropertyThread.onUpdateProp();
			// return;
		} else if (file.isDirectory() && !file.isHidden()) {
			siSubDirNum++;
			if (siSubDirNum % 10 == 0)
				sPropertyThread.onUpdateProp();
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (sPropertyThread != null && sPropertyThread.isStop()) {
					break;
				}
				getProperty(files[i]);
			}
		}
		
		if (null != sPropertyThread && !sPropertyThread.isStop())
			sPropertyThread.onUpdateProp();
	}
		
	public static void showProp(BaseActivity activity, File file) {
		Log.i(TAG, "==> showProp");

		if (null == activity 
				|| null == file || !file.exists()) {
			Log.e(TAG, "invalid parameter in showProp");
			return;
		}else {
			sActivity = activity;
			initPropertyData(file);
			showPropertyDialog(file);
			getPropertyInThread(file);
		}
	}
	
	private static void showPropertyDialog(File file) {
		LayoutInflater inflater = LayoutInflater.from(sActivity);
//		View content = inflater.inflate(R.layout.file_attri, null);
		View content = inflater.inflate(R.layout.file_property, null);
		TextView mFileName = (TextView) content.findViewById(R.id.filename);
		stvSubDirNum = (TextView) content.findViewById(R.id.subdir);
		stvSubFileNum = (TextView) content.findViewById(R.id.subfile);
		stvFileSize = (TextView) content.findViewById(R.id.totalsize);
		TextView mFileType = (TextView) content.findViewById(R.id.filetype);
		TextView mUpdateTime = (TextView) content.findViewById(R.id.updatetime);
	
		// set file time
		Date modifTime = new Date(file.lastModified());
		String modiftime = DateFormat.getTimeFormat(Global.getContext())
				.format(modifTime);
		String modifdate = DateFormat.getDateFormat(Global.getContext())
				.format(modifTime);
		String arrfilename = file.getName();
		String arrmodifyDate = modifdate + " " + modiftime;

		mUpdateTime.setText(arrmodifyDate);

		// set file name
		mFileName.setText(arrfilename);

		// show different content according to file type
		View filetype = content.findViewById(R.id.fa_tr_file_type);//filetype
		View foldernum = content.findViewById(R.id.fa_tr_folder_num);//subdir
		View filenum = content.findViewById(R.id.fa_tr_file_num);//subfile

		if (file.isDirectory()) {
			foldernum.setVisibility(View.VISIBLE);
			filenum.setVisibility(View.VISIBLE);
			filetype.setVisibility(View.GONE);
		} else if (file.isFile()) {
			foldernum.setVisibility(View.GONE);
			filenum.setVisibility(View.GONE);
			filetype.setVisibility(View.VISIBLE);

			mFileType.setText(FileUtil.getFileTypeString(sActivity, file));
		}
		
		new AlertDialog.Builder(sActivity)
		// .setTitle(getString(R.string.alert_dialog_attribute) +
		// " "+arrfilename)
			.setTitle(sActivity.getString(R.string.alert_dialog_attribute))
			.setView(content)
			.setPositiveButton(R.string.alert_dialog_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						if (sPropertyThread != null)
							sPropertyThread.stopIt(); //
						sPropertyThread = null;
					}
			})
			.show();
	}
	
	private static void initPropertyData(File file) {
		siSubDirNum = 0;
		siSubFileNum = 0;
		slFileSize = 0;				
	}
	
	private static void getPropertyInThread(final File file) {
		sPropertyThread = new FilePropertyThread(sActivity, file); 
		sActivity.addMsgHandler(sUpdateHandler);
		sPropertyThread.start();		
	}
}